<?php

/*
  stats_products_per_month.php, v 1.0 2006/02/21 by wolfram

  Stand-alone Admin tool for osCommerce v2.2-MS2

  Released under the GNU General Public License
*/

  require('includes/application_top.php');

if ($jahr_start == 0) { $jahr_start= date("Y")-1; }
if ($jahr_end == 0) { $jahr_end = date("Y"); }

if ($_GET['prod_num']) $prod_num_chosen = tep_db_prepare_input($_GET['prod_num']);

?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN"> <html <?php echo HTML_PARAMS; ?>> <head> <meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>"> <title><?php echo TITLE; ?></title> <link rel="stylesheet" type="text/css" href="includes/stylesheet.css"> <script type="text/javascript" src="includes/general.js"></script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<!-- header //-->
<?php
  if ($printable != 'on') {
  require(DIR_WS_INCLUDES . 'header.php');
  }; ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr>
<?php
   if ($printable != 'on') {;?>
    <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="1" cellpadding="1" class="columnLeft">
<!-- left_navigation //-->
<?php
 require(DIR_WS_INCLUDES . 'column_left.php'); ?>
<!-- left_navigation_eof //-->
        </table>
<?php }; ?>
</td>
<!-- body_text //-->
    <td width="100%" valign="top"align="center"><table border="0" width="70%" cellspacing="0" cellpadding="2">
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><?php echo HEADING_TITLE; ?><hr></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><table border=0 cellpadding="4" width="100%">
          <tr>
<?php
  $models_query = tep_db_query("select p.products_id, p.products_model, pd.products_id, pd.products_name from " . TABLE_PRODUCTS. " as p, " . TABLE_PRODUCTS_DESCRIPTION . " AS pd WHERE p.products_id = pd.products_id and pd.language_id = ".(int)$languages_id." order by pd.products_name asc");
  while($model = tep_db_fetch_array($models_query)) {
    $models[] = array('id' => $model['products_id'], 'text' => $model['products_name'] . ' - ' . $model ['products_model']);
  }

     echo tep_draw_form('date_range','stats_products_per_month.php' , '', 'get');
     //echo '<td class="main">' . ENTRY_MODEL . '&nbsp;' . tep_draw_input_field('prod_num', $prod_num, 'size="9"') . tep_draw_separator('pixel_trans.gif', '1', '1') . '</td>';
   echo '<td class="main" colspan="3">' . ENTRY_MODEL . '&nbsp;' . tep_draw_pull_down_menu('prod_num', $models) . tep_draw_separator('pixel_trans.gif', '1', '1') . '</td></tr>';
     echo '<tr><td align="right" class="main">' . ENTRY_STARTYEAR . tep_draw_input_field('jahr_start', $jahr_start, 'size="4"') . '&nbsp;</td>';
     echo '<td align="left" class="main">' . ENTRY_TOYEAR . tep_draw_input_field('jahr_end', $jahr_end, 'size="4"') . '</td>';
     //echo ENTRY_PRINTABLE . tep_draw_checkbox_field('printable', $print);
     echo '<td align="right"><input type="submit" value="'. ENTRY_SUBMIT . '"></td></form>';
?>
          </tr>
        </table></td>
      </tr>

<?php
$jahr=$jahr_start-1;
while ($jahr < $jahr_end) {
  $jahr=$jahr+1;
?>
      <tr>
        <td><table border="1" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><?php echo $jahr; ?></td>
          </tr>
          <tr>
            <td valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr class="dataTableHeadingRow">
                <td align="center" class="dataTableHeadingContent"><?php echo TABLE_HEADING_MONTH; ?></td>
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_MODEL; ?></td>
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_PRODUCTS; ?></td>
                <td class="dataTableHeadingContent" align="center"><?php echo TABLE_HEADING_SOLD; ?>&nbsp;</td>
                <td class="dataTableHeadingContent" align="center"><?php echo TABLE_HEADING_QUARTER; ?>&nbsp;</td>
                <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_GROSS; ?>&nbsp;</td>
                <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_QUARTER; ?>&nbsp;</td>
              </tr>
  <?php
// start year
  $monat= 0;
  $totalgross = 0;
  $totalquantity = 0;
  $quartal = 0;
  $quartalsum = 0;
  $quartalgross = 0;

  while ($monat < 12) {
    $monat=$monat+1;
    $start_date = date($jahr.'-'.$monat.'-01');
    $end_date = date($jahr.'-'.$monat.'-31');

    $products_query_raw = "select op.products_id, op.products_model, op.products_name, sum(op.products_quantity) as quantitysum , sum(op.products_price*op.products_quantity)as gross FROM " . TABLE_ORDERS . " as o, " . TABLE_ORDERS_PRODUCTS . " AS op WHERE op.products_id='$prod_num_chosen' AND o.date_purchased BETWEEN '" . $start_date . "' AND '" . $end_date . " 23:59:59' AND o.orders_id = op.orders_id GROUP BY op.products_id";
    $products_query = tep_db_query($products_query_raw);
    $products = tep_db_fetch_array($products_query);

    $totalgross = $totalgross + $products['gross'];
    $totalquantity = $totalquantity + $products['quantitysum'];

    if ($quartal < 3) {
      $quartalsum = $quartalsum + $products['quantitysum'];
      $quartalgross = $quartalgross + $products['gross'];
      $quartal = $quartal + 1;
    } else {
      $quartalsum = 0+$products['quantitysum'];
      $quartalgross = 0+ $products['gross'];
      $quartal = 1;
    }
  ?>
              <tr bgcolor="<?php echo ((++$cnt)%3==0) ? '#e0e0e0' : '#ffffff' ?>">
                <!--td class="dataTableContent"><?php echo $rows  ; ?>.</td-->
                <!--td class="dataTableContent"><?php echo 'von '. $start_date .'<br>bis '. $end_date; ?></td-->
                <td width="65" align="right" class="dataTableContent"><?php echo $monat . '/'. $jahr . tep_draw_separator('pixel_trans.gif', '15', '1'); ; ?></td>
                <td class="dataTableContent"><?php echo $products['products_model']; ?></td>
                <td class="dataTableContent"><?php echo $products['products_name']; ?></td>
                <td class="dataTableContent" align="center"><?php echo $products['quantitysum']; ?></td>
                <td class="dataTableContent" align="center"><?php if ($quartal == 3) { echo $quartalsum; } ?></td>
                <td class="dataTableContent" align="right"><?php echo sprintf("%01.2f", $products['gross']); ?>&nbsp;</td>
                <td class="dataTableContent" align="right"><?php if ($quartal == 3) { echo sprintf("%01.2f", $quartalgross); } ?>&nbsp;</td>
              </tr>
  <?php
  } // end year
  ?>
              <tr>
                <td></td>
                <td></td>
                <td></td>
                <td class="dataTableContent" align="right"><b><?php echo ENTRY_TOTAL; ?></b></td>
                <td class="dataTableContent" align="center"><b><?php echo $totalquantity ; ?></b></td>
                <td></td>
                <td class="dataTableContent" align="right"><b><?php echo sprintf("%01.2f", $totalgross); ?></b>&nbsp;</td>
              </tr>
            </table></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
<?php
}
?>
    </table></td>
<!-- body_text_eof //-->
  </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php
  if ($printable != 'on') {
   require(DIR_WS_INCLUDES . 'footer.php');
  }
?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
